package net.roseparadise.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.roseparadise.config.PropertiesConfig;
import net.roseparadise.exception.RightsNotEnoughException;
import net.roseparadise.model.Album;
import net.roseparadise.service.PhotoService;
import net.roseparadise.util.VOUtils;
import net.roseparadise.vo.UserVO;

import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/action/PhotoManage.htm")
@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "PhotoManageController"), name = "PhotoManageAjax")
public class PhotoManageController {
	@Autowired
	private PhotoService photoService;
	
	@RequestMapping(params = "cmd=managealbum",method= RequestMethod.POST)
	public String manageAlbumList(HttpServletRequest request,  ModelMap map){
		//System.out.println("~~~~~~~~Controller begin~~~~~~~~"+title);
		
//		Authotrity check here 
		
		String isstudio =(String)request.getParameter("isstudio");
		String memberid =(String)request.getParameter("memberid");
		if(null==isstudio || "".equals(isstudio))
		{
			isstudio="N";
		}
		if(null==memberid || "".equals(memberid))
		{
			memberid="0";
			isstudio="Y";
		}		
		map.addAttribute("isstudio", isstudio);
		map.addAttribute("memberid", memberid);
		return "albummanage";
	}
	
	@RequestMapping(params = "cmd=addalbum")
	public void addAlbum(HttpServletRequest request,HttpServletResponse response){
		Integer memberid = null;
		String result = "true";
		JSONObject json =new JSONObject();
		try {
			if(VOUtils.checkCurrentUserRights(request, "Rights.StudioMember"))
			{
				UserVO uservo = (UserVO)request.getSession().getAttribute("currentUser");
				if(null != uservo)
				{
					memberid =uservo.getId();
					String isstudio =VOUtils.validateParameter(request, "isstudio","N");
					String albumname = VOUtils.validateParameter(request, "albumname", "",2,30);
					String albumorder = VOUtils.validateParameter(request, "albumorder", "",1,10);	
					//System.out.println(isstudio);
					if ("on".equals(isstudio)){
						isstudio ="Y";
					}else{
						isstudio ="N";
					}
					
					Album album=new Album(isstudio,memberid,VOUtils.getCurrentDateString(),albumname,Integer.parseInt(albumorder));
					photoService.addAlbum(album);
				}else{
					result = "false";
				}
				
			}
		} catch (Exception e) {
			result = "false";
			e.printStackTrace();
		}
		try {			
			json.put("success", result);
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "cmd=deletealbum")
	public void deleteAlbum(HttpServletRequest request,HttpServletResponse response)throws Exception{
			if(!VOUtils.checkCurrentUserRights(request, "Rights.StudioMember")){
				throw new RightsNotEnoughException();
			}else{				
				String albumId = VOUtils.validateParameter(request, "albumid", "0",1,10);	
				System.out.println(albumId);
				photoService.deleteAlbum(Integer.parseInt(albumId));
				
			}
		
	}
	
	@RequestMapping(params = "cmd=updatealbum")
	public void updateAlbum(HttpServletRequest request,HttpServletResponse response)throws Exception{
			if(!VOUtils.checkCurrentUserRights(request, "Rights.StudioMember")){
				throw new RightsNotEnoughException();
			}else{				
				String albumId = VOUtils.validateParameter(request, "albumid", "0",1,10);	
				String field = VOUtils.validateParameter(request, "field", "0");	
				String value = VOUtils.validateParameter(request, "value", "0");	
				photoService.updateAlbum(Integer.parseInt(albumId), field, value);
				
			}
		
	}
	@RequestMapping(params = "cmd=getalbumlist")
	public void getAlbumList(HttpServletRequest request,HttpServletResponse response){
		JSONArray jsonResult = null;
		JSONObject json =new JSONObject();
		try {
			
			String isstudio =VOUtils.validateParameter(request,"isstudio","N");
			String memberid =(String)request.getParameter("memberid");
			String start = VOUtils.validateParameter(request,"start","0");
			
			if(null==memberid || "".equals(memberid))
			{
				memberid="0";
				isstudio="Y";
			}
			
			int maxCount = PropertiesConfig.getInstance().getInt("ListSize.AlbumManage");
			List<Album> list = photoService.getAlbumListManage(Integer.parseInt(memberid), isstudio, Integer.parseInt(start),maxCount);
			int count = photoService.countAlbumListManage(Integer.parseInt(memberid), isstudio);
			
			jsonResult = new JSONArray();			
			for(int i=0; i<list.size();i++)				
			{				
				jsonResult.put(i,new JSONObject((Album)list.get(i)));
			}			
			json.put("data", jsonResult);
			json.put("count", count);
			
			response.getWriter().write(json.toString());
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
